/**
 * 
 */
package br.com.locasis.view.converter;

import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import br.com.locasis.utilitarios.UtilString;

@org.jboss.seam.annotations.faces.Converter
@Name("bigDecimalMascaraConverter")
@BypassInterceptors
public class BigDecimalMascaraConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
        BigDecimal instanciaBigDecimal = null;
        
        if(value == null || value.trim().length() == 0)
            return instanciaBigDecimal;
        
        value = value.replace(".", "");
        value = value.replace(",", ".");
        instanciaBigDecimal = new BigDecimal(value);
        return instanciaBigDecimal;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
        if (!(value instanceof BigDecimal)) {
            return "";
        }
        return UtilString.getInstance().formatarValor(((BigDecimal)value).setScale(2, RoundingMode.CEILING).toString(), "##.###.###.###,##");
    }
}
